import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:app/generated/app_strings.dart';

import '../../../utils/unit_utils.dart';
import '../worksdo/material_requisition_bean.dart';

///采购单信息
class PurOrderInfoBean extends BaseBean {
  String? SupplierName = ""; //供应商
  List<String>? FilePatch = null;
  String? IsSave = "";
  String? Id = "";
  String? PurOrderCode = ""; //采购单号
  String? MaterialType = ""; //物料类型 "MaterialType": "MT01",
  String? Creater = "";
  String? CreaterDateTime = "";
  String? PurStartDate = "";
  String? PurEndDate = "";
  String? SupplierId = "";
  String? FactoryId = "";
  String? FactoryName = "";
  String? Status = "";
  String? OrderType = ""; //采购类型
  String getOrderType(BuildContext context) {
    if (OrderType == "1") {
      return AppStrings.of(context).OrderType1; //大额采购
    } else if (OrderType == "2") {
      return AppStrings.of(context).OrderType2; //零星采购
    }
    return "";
  }

  ///判断是否为大额采购
  bool isOrderType1() {
    if (OrderType == "1") {
      return true;
    }
    return false;
  }

  String? BeiZhu = ""; //备注
  String? SContractCode = "";
  String? ProjectId = "";
  String? ProjectName = "";
  String? IsBind = "";
  String? CurrencyType = ""; //币种
  String? getCurrencyType(BuildContext context) {
    if (StringUtils.isEmpty(CurrencyType)) {
      return AppStrings.of(context).RMB;
      // return "人民币";//特殊处理，如果为空，则显示人民币。
    }
    return CurrencyType;
  }

  String? TaxCode = ""; //税码
  String? TaxRate = ""; //税率
  String? Rate = ""; //汇率
  String? IsChange = "";
  List<PurOrderListBean>? PurOrderList = null;

  ///审核日志
  List<WFLog>? WFLogList = null;

  ///流程节点
  List<NextNodeBean>? NextNode = null;

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    SupplierName = json['SupplierName']?.toString() ?? "";
    FilePatch = setGenericFromJson(json, "FilePatch", <String>[], "");
    IsSave = json['IsSave']?.toString() ?? "";
    Id = json['Id']?.toString() ?? "";
    PurOrderCode = json['PurOrderCode']?.toString() ?? "";
    MaterialType = json['MaterialType']?.toString() ?? "";
    Creater = json['Creater']?.toString() ?? "";
    CreaterDateTime = json['CreaterDateTime']?.toString() ?? "";
    PurStartDate = json['PurStartDate']?.toString() ?? "";
    PurEndDate = json['PurEndDate']?.toString() ?? "";
    SupplierId = json['SupplierId']?.toString() ?? "";
    FactoryId = json['FactoryId']?.toString() ?? "";
    FactoryName = json['FactoryName']?.toString() ?? "";
    Status = json['Status']?.toString() ?? "";
    OrderType = json['OrderType']?.toString() ?? "";
    BeiZhu = json['BeiZhu']?.toString() ?? "";
    SContractCode = json['SContractCode']?.toString() ?? "";
    ProjectId = json['ProjectId']?.toString() ?? "";
    ProjectName = json['ProjectName']?.toString() ?? "";
    IsBind = json['IsBind']?.toString() ?? "";
    CurrencyType = json['CurrencyType']?.toString() ?? "";
    TaxCode = json['TaxCode']?.toString() ?? "";
    TaxRate = json['TaxRate']?.toString() ?? "";
    Rate = json['Rate']?.toString() ?? "";
    IsChange = json['IsChange']?.toString() ?? "";
    PurOrderList = setGenericFromJson(
        json, "PurOrderList", <PurOrderListBean>[], PurOrderListBean());
    WFLogList = setGenericFromJson(json, "WFLogList", <WFLog>[], WFLog());
    if (WFLogList == null) {
      WFLogList = [];
    }
    NextNode =
        setGenericFromJson(json, "NextNode", <NextNodeBean>[], NextNodeBean());
  }

  @override
  instance() {
    // TODO: implement instance
    return PurOrderInfoBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["SupplierName"] = SupplierName;
    map["FilePatch"] = FilePatch;
    map["IsSave"] = IsSave;
    map["Id"] = Id;
    map["PurOrderCode"] = PurOrderCode;
    map["MaterialType"] = MaterialType;
    map["Creater"] = Creater;
    map["CreaterDateTime"] = CreaterDateTime;
    map["PurStartDate"] = PurStartDate;
    map["PurEndDate"] = PurEndDate;
    map["SupplierId"] = SupplierId;
    map["FactoryId"] = FactoryId;
    map["FactoryName"] = FactoryName;
    map["Status"] = Status;
    map["OrderType"] = OrderType;
    map["BeiZhu"] = BeiZhu;
    map["SContractCode"] = SContractCode;
    map["ProjectId"] = ProjectId;
    map["ProjectName"] = ProjectName;
    map["IsBind"] = IsBind;
    map["CurrencyType"] = CurrencyType;
    map["TaxCode"] = TaxCode;
    map["TaxRate"] = TaxRate;
    map["Rate"] = Rate;
    map["IsChange"] = IsChange;
    map["PurOrderList"] = PurOrderList;
    map["WFLogList"] = WFLogList;
    map["NextNode"] = NextNode;
    return map;
  }
}

///物料信息
class PurOrderListBean extends BaseBean {
  String? StockNum = ""; //当前库存
  String? DemandPersonName = "";
  String? DemandDeptName = "";
  String? ChangeManufacturerId = "";
  String? ChangeManufacturerName = "";
  String? ChangePrice = "";
  String? ChangeQualityGrade = "";
  String? ChangeQualityGradeName = "";
  String? IsChange = "";

  ///fixme（1和0）判断；为1时显示；价格，质量等级和品牌；如果为0；就不用做变红的判断了。为1才需要。
  bool isChange() {
    if (IsChange == "1") {
      return true;
    }
    return false;
  }

  // PDMinNum请购数量  MinNum申请数量   Quantity已采购数量
  String? PDMinNum = ""; //请购数量
  String? Quantity = ""; //已采购数量
  String? MinNum = ""; //申请数量
  String? MainUnit = "";

  ///审核日志
  List<WFLog>? WFLogList = null;
  String? Lengths = "";
  String? Numbers = "";
  String? CanWrite = "";
  String? Isquantity = "";
  String? Iswidth = "";
  String? Isheight = "";
  String? Isthickness = "";
  String? Islength = "";
  String? MaterialType = "";
  String? ApplyNum = "";
  String? ListId = "";
  String? POId = "";
  String? PDListId = "";
  String? MaterialTypeId = "";
  String? MaterialCode = "";
  String? MaterialName = "";
  String? Width = "";
  String? Height = "";
  String? Thickness = "";
  String? Size = "";
  String? SubsidiaryNum = ""; //辅数量，一般为原片的面积。
  String? SubsidiaryUnit = ""; //面积单位，如：㎡
  String? Price = ""; //单价
  String? Amount = ""; //金额
  String? ManufacturerId = "";
  String? ManufacturerName = "";
  String? PurCode = "";
  String? ProductId = "";
  String? SourceType = ""; //来源类型
  String? QualityGrade = "";
  String? QualityGradeName = "";

  ///判断是否为原片
  bool isMT01() {
    if (MaterialType == PurDemand.MT01) {
      return true;
    }
    return false;
  }

  ///判断是否为辅料
  bool isMT02() {
    if (MaterialType == PurDemand.MT02) {
      return true;
    }
    return false;
  }

  /// 是否显示宽
  bool Iswidth2() {
    return true;
  }

  /// 是否显示高
  bool Isheight2() {
    return true;
  }

  bool _Isthickness() {
    if (StringUtils.isNotEmpty(Isthickness)) {
      if (Isthickness!.toLowerCase() == "true") {
        return true;
      }
    }
    return false;
  }

  /// 是否显示厚
  bool Isthickness2() {
    // if (_Isthickness()) {//fixme 采购不根据这个参数判断。
    //   return true;
    // } else if (!isMT01()) {
    //   return true;
    // }
    // return false;
    if (isMT01()) {
      return false; //原片不显示
    }
    return true;
  }

  bool _Islength() {
    if (StringUtils.isNotEmpty(Islength)) {
      if (Islength!.toLowerCase() == "true") {
        return true;
      }
    }
    return false;
  }

  /// 是否显示长
  bool Islength2() {
    // if (_Islength()) {
    //   return true;
    // } else if (!isMT01()) {
    //   return true;
    // }
    // return false;
    if (isMT01()) {
      return false; //原片不显示
    }
    return true;
  }

  bool _Isquantity() {
    if (StringUtils.isNotEmpty(Isquantity)) {
      if (Isquantity!.toLowerCase() == "true") {
        return true;
      }
    }
    return false;
  }

  ///是否显示数量
  bool Isquantity2() {
    // if (_Isquantity()) {
    //   return true;
    // } else if (!isMT01()) {
    //   return true;
    // }
    // return false;
    if (isMT01()) {
      return false; //原片不显示
    }
    return true;
  }

  ///是否显示辅数量（面积）
  bool isSubsidiaryNum() {
    /// 物料类型  MT01：原片 MT02：辅料 MT03：五金
    //String? MaterialType = "";
    if (MaterialType == PurDemand.MT01) {
      return true;
    }
    return false;
  }

  // ///是否显示来源类型
  // bool isSourceType() {
  //   if (MaterialType == PurDemand.MT01) {
  //     return false; //原片没有
  //   }
  //   return true;
  // }
  //
  // ///是否显示单价
  // bool isPrice() {
  //   if (MaterialType == PurDemand.MT01) {
  //     return false; //原片没有
  //   }
  //   return true;
  // }
  //
  // ///是否显示金额
  // bool isAmount() {
  //   if (MaterialType == PurDemand.MT01) {
  //     return false; //原片没有
  //   }
  //   return true;
  // }

  String getMaterialTypeName(BuildContext context) {
    if (MaterialType == "MT01") {
      return AppStrings.of(context).Raw; //原片
    } else if (MaterialType == "MT02") {
      return AppStrings.of(context).SupplementaryMaterial; //辅料
    } else if (MaterialType == "MT03") {
      return AppStrings.of(context).Hardware; //五金
    }
    return "";
  }

  String? ListBeizhu = ""; //备注

  ///新加字段；
  String? PackageUnit = ""; //包的单位
  String? WidthUnit = ""; //宽单位
  String? HeightUnit = ""; //高单位
  String? LengthUnit = ""; //长单位
  String? ThicknessUnit = ""; //厚度单位
  String? PackageCount = ""; //包数量
  String? PackageListCount = ""; //每包数量

  //数量单位
  String? QuantityUnit = "";

  ///获取数量，并且带单位。
  String? getNumbersStrAndUnit() {
    // return "${AppUnitUtils.getStringAs0(Numbers)}${QuantityUnit}";
    return "${StringUtils.toStringAsDecimal(Numbers)}${QuantityUnit}";
  }

  double toDouble(String? num) {
    if (StringUtils.isEmpty(num)) {
      return 1;
    } else {
      try {
        var d = double.parse(num!);
        if (d <= 0) {
          return 1; //小于等于0的作1处理。
        }
        return d;
      } catch (e) {}
    }
    return 1;
  }

  String? WidthInput = ""; //记录手动输入的。(自己加的本地参数)
  double Width2() {
    var num = Width ?? "1";
    return toDouble(num);
  }
  ///获取宽；
  String? getWidthInput() {
    if (StringUtils.isEmpty(WidthInput)) {
      //Width:============================0.0
      //print("Width:============================${Width}");
      //fixme 毫米转化为其他对应的单位；服务器默认传递的都是毫米。最后我们提交的时候，还需要再转化为毫米再提交给服务器。
      if (StringUtils.isEmpty(Width) || Width == "0" || Width == "0.0") {
        WidthInput = "0";
      } else {
        WidthInput =
            AppUnitUtils.mmtoUnit(WidthUnit, Width2().toString()).toString();
        WidthInput = StringUtils.toStringAsDecimal(WidthInput);
      }
    }
    return WidthInput;
  }

  double Height2() {
    var num = Height ?? "1";
    return toDouble(num);
  }

  String? HeightInput = ""; //记录手动输入的。(自己加的本地参数)
  ///获取高；
  String? getHeightInput() {
    if (StringUtils.isEmpty(HeightInput)) {
      //毫米转化为其他对应的单位；服务器默认传递的都是毫米。
      if (StringUtils.isEmpty(Height) || Height == "0" || Height == "0.0") {
        HeightInput = "0";
      } else {
        HeightInput =
            AppUnitUtils.mmtoUnit(HeightUnit, Height2().toString()).toString();
        HeightInput = StringUtils.toStringAsDecimal(HeightInput);
      }
    }
    return HeightInput;
  }

  double Lengths2() {
    var num = Lengths ?? "1";
    return toDouble(num);
  }

  String? LengthsInput = ""; //记录手动输入的。(自己加的本地参数)
  ///获取长；
  String? getLengthsInput() {
    if (StringUtils.isEmpty(LengthsInput)) {
      //毫米转化为其他对应的单位；服务器默认传递的都是毫米。
      if (StringUtils.isEmpty(Lengths) || Lengths == "0" || Lengths == "0.0") {
        LengthsInput = "0";
      } else {
        LengthsInput =
            AppUnitUtils.mmtoUnit(LengthUnit, Lengths2().toString()).toString();
        LengthsInput = StringUtils.toStringAsDecimal(LengthsInput);
      }
    }
    return LengthsInput;
  }

  double Thickness2() {
    var num = Thickness ?? "1";
    return toDouble(num);
  }

  String? ThicknessInput = ""; //记录手动输入的。(自己加的本地参数)
  ///获取厚；
  String? getThicknessInput() {
    if (StringUtils.isEmpty(ThicknessInput)) {
      //毫米转化为其他对应的单位；服务器默认传递的都是毫米。
      if (StringUtils.isEmpty(Thickness) ||
          Thickness == "0" ||
          Thickness == "0.0") {
        ThicknessInput = "0";
      } else {
        ThicknessInput =
            AppUnitUtils.mmtoUnit(ThicknessUnit, Thickness2().toString())
                .toString();
        ThicknessInput = StringUtils.toStringAsDecimal(ThicknessInput);
      }
    }
    return ThicknessInput;
  }

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    StockNum = json['StockNum']?.toString() ?? "";
    DemandPersonName = json['DemandPersonName']?.toString() ?? "";
    DemandDeptName = json['DemandDeptName']?.toString() ?? "";
    ChangeManufacturerId = json['ChangeManufacturerId']?.toString() ?? "";
    ChangeManufacturerName = json['ChangeManufacturerName']?.toString() ?? "";
    ChangePrice = json['ChangePrice']?.toString() ?? "";
    ChangeQualityGrade = json['ChangeQualityGrade']?.toString() ?? "";
    ChangeQualityGradeName = json['ChangeQualityGradeName']?.toString() ?? "";
    IsChange = json['IsChange']?.toString() ?? "";
    PDMinNum = json['PDMinNum']?.toString() ?? "";
    Quantity = json['Quantity']?.toString() ?? "";
    WFLogList = setGenericFromJson(json, "WFLogList", <WFLog>[], WFLog());
    if (WFLogList == null) {
      WFLogList = [];
    }
    Lengths = json['Lengths']?.toString() ?? "";
    Numbers = json['Numbers']?.toString() ?? "";
    CanWrite = json['CanWrite']?.toString() ?? "";
    Isquantity = json['Isquantity']?.toString() ?? "";
    Iswidth = json['Iswidth']?.toString() ?? "";
    Isheight = json['Isheight']?.toString() ?? "";
    Isthickness = json['Isthickness']?.toString() ?? "";
    Islength = json['Islength']?.toString() ?? "";
    MaterialType = json['MaterialType']?.toString() ?? "";
    ApplyNum = json['ApplyNum']?.toString() ?? "";
    ListId = json['ListId']?.toString() ?? "";
    POId = json['POId']?.toString() ?? "";
    PDListId = json['PDListId']?.toString() ?? "";
    MaterialTypeId = json['MaterialTypeId']?.toString() ?? "";
    MaterialCode = json['MaterialCode']?.toString() ?? "";
    MaterialName = json['MaterialName']?.toString() ?? "";
    Width = json['Width']?.toString() ?? "";
    Height = json['Height']?.toString() ?? "";
    Thickness = json['Thickness']?.toString() ?? "";
    Size = json['Size']?.toString() ?? "";
    MinNum = json['MinNum']?.toString() ?? "";
    MainUnit = json['MainUnit']?.toString() ?? "";
    SubsidiaryNum = json['SubsidiaryNum']?.toString() ?? "";
    SubsidiaryUnit = json['SubsidiaryUnit']?.toString() ?? "";
    Price = json['Price']?.toString() ?? "";
    Amount = json['Amount']?.toString() ?? "";
    ManufacturerId = json['ManufacturerId']?.toString() ?? "";
    ManufacturerName = json['ManufacturerName']?.toString() ?? "";
    PurCode = json['PurCode']?.toString() ?? "";
    ProductId = json['ProductId']?.toString() ?? "";
    SourceType = json['SourceType']?.toString() ?? "";
    QualityGrade = json['QualityGrade']?.toString() ?? "";
    QualityGradeName = json['QualityGradeName']?.toString() ?? "";
    ListBeizhu = json['ListBeizhu']?.toString() ?? "";
    //江勇新加字段
    PackageUnit = json["PackageUnit"]?.toString() ?? "";
    WidthUnit = json["WidthUnit"]?.toString() ?? "";
    HeightUnit = json["HeightUnit"]?.toString() ?? "";
    LengthUnit = json["LengthUnit"]?.toString() ?? "";
    ThicknessUnit = json["ThicknessUnit"]?.toString() ?? "";
    PackageCount = json["PackageCount"]?.toString() ?? "";
    PackageListCount = json["PackageListCount"]?.toString() ?? "";
    QuantityUnit= json["QuantityUnit"]?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return PurOrderListBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["StockNum"] = StockNum;
    map["DemandPersonName"] = DemandPersonName;
    map["DemandDeptName"] = DemandDeptName;
    map["ChangeManufacturerId"] = ChangeManufacturerId;
    map["ChangeManufacturerName"] = ChangeManufacturerName;
    map["ChangePrice"] = ChangePrice;
    map["ChangeQualityGrade"] = ChangeQualityGrade;
    map["ChangeQualityGradeName"] = ChangeQualityGradeName;
    map["IsChange"] = IsChange;
    map["PDMinNum"] = PDMinNum;
    map["Quantity"] = Quantity;
    map["WFLogList"] = WFLogList;
    map["Lengths"] = Lengths;
    map["Numbers"] = Numbers;
    map["CanWrite"] = CanWrite;
    map["Isquantity"] = Isquantity;
    map["Iswidth"] = Iswidth;
    map["Isheight"] = Isheight;
    map["Isthickness"] = Isthickness;
    map["Islength"] = Islength;
    map["MaterialType"] = MaterialType;
    map["ApplyNum"] = ApplyNum;
    map["ListId"] = ListId;
    map["POId"] = POId;
    map["PDListId"] = PDListId;
    map["MaterialTypeId"] = MaterialTypeId;
    map["MaterialCode"] = MaterialCode;
    map["MaterialName"] = MaterialName;
    map["Width"] = Width;
    map["Height"] = Height;
    map["Thickness"] = Thickness;
    map["Size"] = Size;
    map["MinNum"] = MinNum;
    map["MainUnit"] = MainUnit;
    map["SubsidiaryNum"] = SubsidiaryNum;
    map["SubsidiaryUnit"] = SubsidiaryUnit;
    map["Price"] = Price;
    map["Amount"] = Amount;
    map["ManufacturerId"] = ManufacturerId;
    map["ManufacturerName"] = ManufacturerName;
    map["PurCode"] = PurCode;
    map["ProductId"] = ProductId;
    map["SourceType"] = SourceType;
    map["QualityGrade"] = QualityGrade;
    map["QualityGradeName"] = QualityGradeName;
    map["ListBeizhu"] = ListBeizhu;
    //江勇新加字段
    map["PackageUnit"] = PackageUnit;
    map["WidthUnit"] = WidthUnit;
    map["HeightUnit"] = HeightUnit;
    map["LengthUnit"] = LengthUnit;
    map["ThicknessUnit"] = ThicknessUnit;
    map["PackageCount"] = PackageCount;
    map["PackageListCount"] = PackageListCount;
    map["QuantityUnit"] = QuantityUnit;
    return map;
  }
}

///流程节点
class NextNodeBean extends RollerBean {
  String? ItmeName = "";
  String? ItmeId = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    ItmeName = json['ItmeName']?.toString() ?? "";
    ItmeId = json['ItmeId']?.toString() ?? "";
    key = ItmeId;
    value = ItmeName;
  }

  @override
  instance() {
    // TODO: implement instance
    return NextNodeBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["ItmeName"] = ItmeName;
    map["ItmeId"] = ItmeId;
    return map;
  }
}
